namespace Nice157Advices.Advices
{
    /// <summary>
    /// 34、为泛型参数设定约束
    /// </summary>
    public static class _34
    {
        public static void Go ()
        {
            var s1 = new Salary { Name = "capdiem", Age = 24, BaseSalary = 3000, Bouns = 5000 };
            var s2 = new Salary { Name = "fox", Age = 22, BaseSalary = 3000, Bouns = 6000 };

            var computer = new AgeComputer ();
            var result = computer.Compare<Salary> (s1, s2);
            System.Console.WriteLine (result);

            var e1 = new Empolyee { Name = "capdiem", Age = 24 };
            var e2 = new Empolyee { Name = "fox", Age = 21 };
            System.Console.WriteLine (computer.Compare<Empolyee> (e1, e2));
        }

        private class Salary : Empolyee
        {
            public int BaseSalary { get; set; }
            public int Bouns { get; set; }
        }

        private class Empolyee
        {
            public string Name { get; set; }
            public int Age { get; set; }
        }

        private class AgeComputer
        {
            /**
             * 1.指定参数是值类型（除Nullable外） struct...
             * 2.指定参数是引用类型 class
             * 3.指定参数具有无参数的公共构造函数 new()
             * 4.指定参数必须是指定的基类、或者派生自指定的基类
             * 5.指定参数必须是指定的接口、或者实现指定的接口
             * 6.指定T提供的类型参数必须是为U提供的参数，或者派生自为U提供的参数
             * 7.可以对同一类型的参数设置多个约束，并且约束自身可以是泛型类型
             */
            public int Compare<T> (T t1, T t2) where T : Empolyee
            {
                if (t1.Age > t2.Age)
                {
                    return 1;
                }
                else if (t1.Age == t2.Age)
                {
                    return 0;
                }
                else
                {
                    return -1;
                }
            }
        }
    }
}